package com.example.newsmanager.config;

import com.example.newsmanager.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private RedisTemplate<String,Object> template;

    @Bean
    public JwtInterceptor jwtInterceptor(){
        return new JwtInterceptor(template);
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器，并指定拦截的路径
        registry.addInterceptor(jwtInterceptor()).addPathPatterns(
                "/news/publish","/news/userList","/news/collectList",
                "/comment/add",
                "/user/my_info","/user/update",
                "/collect/add","/collect/isCollected","/collect/delete","/user/update_auth",
                "/user/update_password","/user/logout");
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173")
                .allowedMethods("GET", "POST","DELETE","PUT")
                .allowCredentials(true);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/**") //虚拟url路径
                .addResourceLocations("file:/project/files/"); //真实本地路径
    }

}
